package com.dj.mingkong.admin.biz.repository.database.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.admin.biz.controller.system.vo.request.RoleQueryVo;
import com.dj.mingkong.admin.biz.util.WrapperUtil;
import com.dj.mingkong.common.infrastructure.base.dto.query.QueryVo;
import com.dj.mingkong.common.infrastructure.base.repository.BaseDatabaseRepository;
import com.dj.mingkong.common.infrastructure.eo.database.system.RoleEo;
import com.dj.mingkong.admin.biz.repository.database.system.mapper.RoleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;


/**
 * 角色repository
 *
 * @author zenglonghui
 * @date: 2022/5/18 11:02
 */
@Repository
public class RoleRepository extends BaseDatabaseRepository<RoleMapper, RoleEo, QueryVo> {
    public Page<RoleEo> listPageByVo(RoleQueryVo queryVo) {
        final QueryWrapper<RoleEo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotBlank(queryVo.getName()),
                        RoleEo::getName, queryVo.getName())
                .like(StringUtils.isNotBlank(queryVo.getCode()),
                        RoleEo::getCode, queryVo.getCode())
                .eq(StringUtils.isNotBlank(queryVo.getType()),
                        RoleEo::getType, queryVo.getType())
                .eq(StringUtils.isNotBlank(queryVo.getStatus()),
                        RoleEo::getStatus, queryVo.getStatus());
        WrapperUtil.buildSort(queryWrapper, queryVo.getSort());
        return listPage(queryWrapper, queryVo.getPage(), queryVo.getPageSize());
    }

    public RoleEo oneByCode(String code) {
        return lambdaQuery().eq(RoleEo::getCode, code).one();
    }
}
